package com.example.natatorium_management.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.natatorium_management.entity.User;
import com.example.natatorium_management.mapper.UserMapper;
import com.example.natatorium_management.result.restful.R;
import com.example.natatorium_management.result.restful.ResponseEunm;
import com.example.natatorium_management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService{
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }


    /**
     * 登录验证
     * @param user 用户
     * @return R 结果集
     */
    @Override
    public R<User> logon(User user) {
        User register = userMapper.logon(user);
        if (register == null) {
            return new R<>(ResponseEunm.USERNAME_OR_PASSWORD_INVALIDATE,register);
        }
        return new R<>(ResponseEunm.SUCCESS,register);
    }

    /**
     * 注册
     * @param user 注册
     */
    @Override
    public R<User> register(User user) {
        User register = userMapper.registerFirst(user);
        if(register == null){
            userMapper.registerSecond(user);
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.USER_ALREADY_EXISTS,null);
    }

    /**
     * 修改密码
     * @param user e
     * @return e
     */
    @Override
    public R<User> updatePassword(User user) {
        if(user.getPassword() == null || user.getPassword().equals("")){
            return new R<>(ResponseEunm.PASSWORD_CANNOT_NULL,null);
        }
        userMapper.updatePassword(user);
        return new R<>(ResponseEunm.SUCCESS,null);
    }
}
